package com.example.a2032.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.example.a2032.R;
import com.example.a2032.Utils.BleSdk;
import com.example.a2032.Utils.SchedulersTransformer;
import com.example.a2032.entity.RealTimeStepData;

import java.util.Calendar;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;

/**
 * 自定义手表显示界面
 * A simple {@link Fragment} subclass.
 * Use the {@link WatchBgFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class WatchBgFragment extends BaseFragment {

    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.MyWatchView)
    com.example.a2032.view.MyWatchView MyWatchView;

    // TODO: Rename and change types of parameters
    private String mParam1;
    private String mParam2;

    public WatchBgFragment() {
        // Required empty public constructor
    }

    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *

     * @return A new instance of fragment WatchBgFragment.
     */
    // TODO: Rename and change types and number of parameters
    public static WatchBgFragment newInstance() {
        WatchBgFragment fragment = new WatchBgFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_watch_bg, container, false);
        ButterKnife.bind(this, view);
        init();
        return view;
    }

    private void init() {
        startDateTimer();
        subscribe(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
        unSubscribe(timerDisposable);
    }

    Disposable timerDisposable;

    private void startDateTimer() {
        Observable.interval(1, TimeUnit.SECONDS).compose(SchedulersTransformer.io2MainObservable()).subscribe(new Observer<Long>() {
            @Override
            public void onSubscribe(Disposable d) {
                timerDisposable = d;
            }

            @Override
            public void onNext(Long value) {
                showDate();
            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onComplete() {

            }
        });
    }

    @Override
    public void RealTimeData(RealTimeStepData timeStepData) {
        super.RealTimeData(timeStepData);
        int step = timeStepData.getStep();
    }

    private void showDate() {
        MyWatchView.changeTime(Calendar.getInstance());
    }

    private static final String TAG = "WatchBgFragment";

    @Override
    public void onResume() {
        super.onResume();
        Log.i(TAG, "onResume: ");
        sendData(BleSdk.enableRealTimeStep(true, true));
        ;
    }
}